package com.huiyeji.hui.controller;

import com.huiyeji.hui.auth.Authenticator;
import com.huiyeji.hui.auth.License;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.service.ApplicationService;
import com.huiyeji.hui.utils.HuiResponse;
import com.huiyeji.hui.utils.SuccessEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author zhaoduo
 */
@RestController
@RequestMapping("/relation")
public class RelationController {

    private final ApplicationService service;

    private final Authenticator authenticator = Authenticator.getInstance();

    @Autowired
    public RelationController(ApplicationService service){
        this.service = service;
    }

    @DeleteMapping("/user/deleteFriend/{fid}")
    public HuiResponse<?> deleteFriend(HttpSession session,
                                       @PathVariable("fid")Long fid){
        User user = authenticator.check(session, License.GENERAL);
        service.userDeleteFriend(user.getUid(),fid);
        return HuiResponse.ok(SuccessEnum.DELETE_FRIEND_SUCCESS);
    }

    @PostMapping("/user/careFriend/{fid}")
    public HuiResponse<?> careFriend(HttpSession session,
                                     @PathVariable("fid")Long fid){
        User user = authenticator.check(session, License.GENERAL);
        service.userCareFriend(user.getUid(),fid);
        return HuiResponse.ok(SuccessEnum.CARE_FRIEND_SUCCESS);
    }

    @PostMapping("/user/putBlackList/{fid}")
    public HuiResponse<?> putFriendIntoBlackList(HttpSession session,
                                                 @PathVariable("fid")Long fid){
        User user = authenticator.check(session, License.GENERAL);
        service.putFriendIntoBlackList(user.getUid(),fid);
        return HuiResponse.ok(SuccessEnum.PUT_INTO_BLACK_LIST_SUCCESS);
    }

    @PostMapping("/user/changeRemark")
    public HuiResponse<?> changeRemark(@RequestParam("oid")Long oid,
                                       @RequestParam("remark")String remark,
                                       HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.userChangeRemark(user.getUid(),oid,remark);
        return HuiResponse.ok(SuccessEnum.MODIFY_SUCCESS);
    }

    @PostMapping("/user/recoverNormalRelation/{oid}")
    public HuiResponse<?> recoverNormalRelation(@PathVariable("oid")Long oid,
                                                HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.userRelationRecoverNormal(user.getUid(),oid);
        return HuiResponse.ok(SuccessEnum.USER_RELATION_RECOVER_NORMAL_SUCCESS);
    }


    //------------------------------------------  group methods -------------------------------------------------------

    @DeleteMapping("/group/exitGroup/{gid}")
    public HuiResponse<?> exitGroup(@PathVariable("gid")Long gid,
                                    HttpSession session){
        User user = authenticator.check(session, License.GENERAL);
        service.userExitGroup(user.getUid(),gid);
        return HuiResponse.ok(SuccessEnum.EXIT_GROUP_SUCCESS);
    }

    @DeleteMapping("/group/kickOutOther")
    public HuiResponse<?> kickOutOther(HttpSession session,
                                       @RequestParam("gid")Long gid,
                                       @RequestParam("oid")Long oid){
        User user = authenticator.check(session, License.GENERAL);
        service.kickOutOtherFromGroup(user.getUid(),gid,oid);
        return HuiResponse.ok(SuccessEnum.KICK_OUT_SUCCESS);
    }
}
